﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Gugu.management.Util
{
    [Serializable]
    class MainSetting : ISerializable
    {
        public List<string> Params;
        private const string FileName = "Setting.data";

        public MainSetting()
        {
            Params = new List<string> { @"C:\", @"C:\", @"C:\" };
        }

        /// <summary>
        /// get value
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ctxt"></param>
        public MainSetting(SerializationInfo info, StreamingContext ctxt)
        {
            Params = (List<string>)info.GetValue("Params", typeof(List<string>));
        }
        /// <summary>
        /// add value
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ctxt"></param>
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("Params", Params);
        }

        /// <summary>
        /// save file
        /// </summary>
        /// <param name="entry"></param>
        public static void Serialize(MainSetting entry)
        {
            try
            {
                var stream = File.Open(FileName, FileMode.Create);
                var bformatter = new BinaryFormatter();
                bformatter.Serialize(stream, entry);
                stream.Close();
            }
            catch (Exception)
            {
                DeSerialize();
            }
        }

        /// <summary>
        /// load file
        /// </summary>
        /// <returns></returns>
        public static MainSetting DeSerialize()
        {
            var entry = new MainSetting();
            try
            {
                //Open the file written above and read values from it.
                var stream = File.Open(FileName, FileMode.Open);
                var bformatter = new BinaryFormatter();
                entry = (MainSetting)bformatter.Deserialize(stream);
                stream.Close();
            }
            catch (Exception)
            {
                Debug.WriteLine("Not setting file");
            }
            return entry;
        }
    }
}
